<?php
declare (strict_types=1);

namespace app\webapi\common;

use library\JwtToken;
use library\OWException;
use think\App;
use think\facade\Request;

/**
 * @基础控制器
 */
abstract class BaseController
{
    /**
     * @var string
     */
    protected $website_id;

    /**
     * @var string[]
     *
     */
    private $apiWhiteList;

    /**
     * @desc 初始化方法
     * @return void
     */
    public function __construct(App $app)
    {
        $this->apiWhiteList = $this->getApiWhiteList();
        $this->initialize();
    }

    /**
     * @return void
     */
    protected function initialize()
    {
        $rule = Request::rule();
        if (!in_array($rule->getRoute(), $this->apiWhiteList)) {
            $token = Request::header('token');
            if (empty($token)) {
                throw new OWException('未检测到token');
            }
            $this->website_id = (new JwtToken())->verifyJwt($token);
        }
    }

    /**
     * @desc 获取路由白名单
     * @return array
     * @author zhaozhiwei
     * @time 2023/12/19-9:30
     */
    private function getApiWhiteList()
    {
        return [
            'webapi/WebsiteController/getToken',
        ];
    }

    /**
     * @desc 成功的响应方法(用于控制器层)
     * @param $data
     * @param $message
     * @return \think\response\Json
     */
    public function responseSuccess($data = [], $message = 'success')
    {
        return show(200, $message, $data);
    }


}
